package cn.etynit.exception;

import cn.etynit.result.JsonResult;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(Exception.class)
    public JsonResult exceptionHandler(Exception e){
        e.printStackTrace();
        return JsonResult.error(GlobalExceptionCodeEnum.ERROR);
    }

    @ExceptionHandler(GlobalException.class)
    public JsonResult globalExceptionHandler(GlobalException e){
        e.printStackTrace();
        return JsonResult.error(e);
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    public JsonResult methodArgumentNotValidExceptionHandler(MethodArgumentNotValidException e){
        e.printStackTrace();
        StringBuilder stringBuilder = new StringBuilder();e.getBindingResult().getAllErrors().forEach(error->{
            stringBuilder.append(error.getDefaultMessage()+"--");
        });
        return JsonResult.error(stringBuilder.toString());
    }
}
